package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;


@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment1 = this.get(appointment.getId());
        // 必须为预约中才可以修改
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment1.getStatus())){
            throw new BusinessException("非法操作,不是预约中,不能编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
           // 根据id把数据查询出来
            Appointment appointment = this.get(dictId);

            // 判断状态不是预约中状态,或取消, 否者就抛出异常
            if ( !Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus()) && !Appointment.STATUS_CANCEL.equals(appointment.getStatus()) ){
                throw new BusinessException("非法操作,不是预约中或已取消,不能删除");
            }

            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    //到店
    @Override
    public void arrivalShop(Long id) {
        // 根据id把当前这一条数据查询出来
        Appointment appointment = this.get(id);
        // 判断状态必须为预约中状态,不是预约中状态就抛出异常
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("非法操作,不是预约中,不能到店");
        }

        // 修改状态为到店状态,修改到店时间
        appointmentMapper.changeArrivalStatus(id,Appointment.STATUS_ARRIVAL);

    }

    //取消
    @Override
    public void cancel(Long id) {
        // 根据id把当前这一条数据查询出来
        Appointment appointment = this.get(id);
        // 判断状态必须为预约中状态,不是预约中状态就抛出异常
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("非法操作,不是预约中,不能取消");
        }

        // 修改状态为取消
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);

    }

    @Override
    public Long generateStatment(Long id) {
        //根据id 把预约单查询出来
        Appointment appointment = this.get(id);
        // 根据预约单id 上结算表中去查询看是否有数据
       Statement statement = statementMapper.queryAppId(id);
       // 如果为空,创建结算单,把数据封装进去,数据来源于预约单
        if (statement == null){
            statement = new Statement();
            statement.setCreateTime(new Date());
            statement.setAppointmentId(id);
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate()); //设置结算单的车牌号码
            statement.setCarSeries(appointment.getCarSeries()); //设置结算单的汽车类型
            statement.setServiceType(appointment.getServiceType()); //服务类型
            statement.setInfo(appointment.getInfo());

            // 把数据保存到数据库中(结算表中)
            statementMapper.insert(statement);
        }
        // 修改预约单状态为已结算状态
        appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);

        return statement.getId(); //返回结算表的id,用于前端回调函数取到
    }


}












